<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Article; 
use App\Comment;
use App\Category;
use App\Notification;

class CategoryController extends Controller
{
    //
    public function index(Request $request){
        $category = $request->get('name');
        $categoryOne = Category::where('name', $category)->first();
        $founder = User::find($categoryOne->founder);
        $articles = Article::where('category', $category)->orderBy('created_at', 'desc')->get();
        return view('category/index',['articles' => $articles, 'category' => $categoryOne, 'founder' => $founder]);
    }

    public function create(){
        return view('category/create');
    }

    public function store(Request $request){
        $this->validate($request, [
            'category'=>'required|unique:articles|max:255',
            'intro'=>'required',
        ]);

        if ($request->hasFile('imgOne')) {
            $file = $request->file('imgOne');
            if($file->isValid()){ 
                $clientName = $file -> getClientOriginalName();    //客户端文件名称..
                // $tmpName = $file ->getFileName();   //缓存在tmp文件夹中的文件名例如php8933.tmp 这种类型的.
                // $realPath = $file -> getRealPath();     //这个表示的是缓存在tmp文件夹下的文件的绝对路径
                $entension = $file -> getClientOriginalExtension();   //上传文件的后缀.
                // $mimeTye = $file -> getMimeType();    //也就是该资源的媒体类型
                $newName = $newName = md5(date('ymdhis').$clientName).".".$entension;    //定义上传文件的新名称
                // $path = $file -> move('storage/uploads',$newName);    //把缓存文件移动到制定文件夹

                $newName = md5(date("Y-m-d-H-i-s").'-'.$clientName).'.'.$entension;
                $file->move(public_path().'/img/category', $newName);
            }
        }

        $category = new Category();
        $category->name = $request->get('category');
        $category->intro = $request->get('intro');
        $category->category_img = $newName;
        $category->founder = $request->user()->id;
        $user = User::find($request->user()->id);
        $no_user_id = $user->no_id;
        $category->follower = $no_user_id.',';
        if($category->save()){
            return redirect('category/home');
        }else{
            return redirect()->back()->withInput()->withErrors('新增失败！');
        }
    }

    public function edit($id) { 
        $category = Category::find($id); 
        return view('category/edit')->with('category', $category); 
    }

    public function show($id){
        $article = Article::find($id);
        $article->read_total += 1;
        $article->save();
        return view('article/show')->withArticle(Article::with('hasManyComments')->with('hasManyUsers')->find($id));
    }

    public function update(Request $request)
    {
        $this->validate($request, [
            // 'category'=>'required|unique:articles|max:255',
            'intro'=>'required',
        ]);

        $category_id = $request->get('category_id');
        $founder = $request->get('founder');
        $category = Category::find($category_id);

        if ($request->hasFile('imgOne')) {
            $file = $request->file('imgOne');
            if($file->isValid()){ 
                $clientName = $file -> getClientOriginalName();    //客户端文件名称..
                // $tmpName = $file ->getFileName();   //缓存在tmp文件夹中的文件名例如php8933.tmp 这种类型的.
                // $realPath = $file -> getRealPath();     //这个表示的是缓存在tmp文件夹下的文件的绝对路径
                $entension = $file -> getClientOriginalExtension();   //上传文件的后缀.
                // $mimeTye = $file -> getMimeType();    //也就是该资源的媒体类型
                $newName = $newName = md5(date('ymdhis').$clientName).".".$entension;    //定义上传文件的新名称
                // $path = $file -> move('storage/uploads',$newName);    //把缓存文件移动到制定文件夹

                $newName = md5(date("Y-m-d-H-i-s").'-'.$clientName).'.'.$entension;
                $file->move(public_path().'/img/category', $newName);
            }
        }
        $category->intro = $request->get('intro');
        $category->category_img = $newName;
        if($category->save()){
            return redirect('/category/user'.'?user='.$founder);
        }else{
            return redirect('/category/user'.'?user='.$founder)->withErrors('修改失败！');
        }
    }

    public function destroy($id){
        $category = Category::find($id);  
        if($category->delete()){
            return redirect('/category')->withErrors('删除成功！');
        }else{
            return redirect('/category')->back()->withInput()->withErrors('删除失败！');
        }
    }

    public function follow(Request $request){
        $category_id = $request->get('category');
        $user_id = $request->user()->id;
        $user = User::find($user_id);
        $no_user_id = $user->no_id;
        $category = Category::find($category_id);

        $user_array = explode(",", $category->follower);
        foreach ($user_array as $array){
            if ($user_id == $array){
                return back()->with('followed', 1);
            }
        }
        $category->follower = $no_user_id.','.$category->follower;     
        // $category_array = explode(",", $user->follow);
        // foreach ($user_array as $array){
        //     if ($user == $array){
        //         return back()->with('followed', 1);
        //     }
        // }
        // $user = User::find($user_id);
        $category->follower_total += 1;
        $category->save();
        // var_dump($category->follower);
        return redirect('/category/home')->with('followed', 1)->with('user_array', $user_array);
    }

    public function unFollow(Request $request){
        $category_id = $request->get('category');
        $user_id = $request->user()->id;
        $user = User::find($user_id);
        $no_user_id = $user->no_id;
        $category = Category::find($category_id);

        $user_array = explode(",", $category->follower);

        foreach ($user_array as $key => $value){
            if ($no_user_id == $value){
                unset($user_array[$key]);  
            }
            // return $user_array;
        }
        // var_dump($user_array);
        $category->follower = implode(',', $user_array);
        $category->follower_total -= 1;
        $category->save();
        // var_dump($category->follower);
        return redirect('/category/home')->with('followed', 0);
    }

    public function homeCategory(Request $request) {
        $categories = Category::all();
        return view('category/home')->with('categories', $categories);
    }

    public function userCategory(Request $request) {
        $founder = $request->get('user');
        $categories = Category::where('founder', $founder)->get();
        return view('category/home')->with('categories', $categories);
    }

}
